// pages/MenberAuth/MenberAuth.js
const app = getApp();
var http = require('../../utils/network.js');
var url = 'index.php/Home/Index/getUserInfo';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  // 事件处理函数
  // 授权
  getUserInfo: function (e) {
    var that = this;
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.login({
            success: function (res) {
              console.log(res)
              var param = {
                code: res.code
              }
              if (res.code) {
                var result = http.get(url, param, true);
                result.then(function (value) {
                  if (value.message == '0') {
                    that.setData({
                      isLogin: true
                    })
                    // 将会员信息保存到本地缓存中去
                    wx.setStorageSync('user_id', value.user_id)
                    wx.setStorageSync('user_name', value.user_name)
                    wx.setStorageSync('user_phone', value.user_phone)
                    wx.setStorageSync('user_integral', value.user_integral)
                    wx.setStorageSync('user_balance', value.user_balance)
                    wx.setStorageSync('is_paidmember', value.is_paidmember)
                  } else if (value.message == '1') {
                    wx.setStorageSync('openid', value.unionid)
                    wx.setStorageSync('openid', value.openid)
                    wx.setStorageSync('session_key', value.session_key)
                    // 获取微信信息 
                    wx.getUserInfo({
                      success: function (userInfo) {
                        console.log(userInfo)
                        that.setData({
                          userInfo: userInfo,
                          isLogin: true
                        })
                      }
                    })
                  }
                })
              }
            },
            fail: function (res) {

            }
          })
        } else {
          // '获取用户信息失败')
        }
      }
    })
  },
  // 获取手机号
  getPhoneNumber(e) {
    var that = this;
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
    console.log(wx.getStorageSync('session_key'))

    var url1 = 'index.php/Home/Index/getTelNumber';
    var param = {
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv,
      session_key: wx.getStorageSync('session_key')
    }

    var result = http.get(url1, param, true);
    result.then(function (res) {
      // 注册新的会员信息，将会员信息保存到数据库中
      var addUserurl = 'index.php/Home/Index/registUser'
      var userInfo = that.data.userInfo;
      var addUserParam = {
        nickname: userInfo.userInfo.nickName,
        sex: userInfo.userInfo.gender,
        headimgurl: userInfo.userInfo.avatarUrl,
        mobile: res.phoneNumber,
        openid: wx.getStorageSync('openid'),
        unionid: wx.getStorageSync('unionid'),
      }
      var addUserResult = http.post(addUserurl, addUserParam, true);
      addUserResult.then(function (value) {
        if (value.message == '0') {
          wx.showToast({
            title: '授权成功',
            icon: 'success',
            duration: 2000,
            success: function (res) {
              // 将会员信息保存到本地缓存中
              wx.setStorageSync('user_id', value.user_id)
              wx.setStorageSync('user_name', value.user_name)
              wx.setStorageSync('user_phone', value.user_phone)
              wx.setStorageSync('user_integral', value.user_integral)
              wx.setStorageSync('user_balance', value.user_balance)
              wx.setStorageSync('is_paidmember', value.is_paidmember)
              wx.navigateBack({
                delta: 1
              })
            }
          })
        } else if (res.message == '1') {
          console.log('注册新用户失败！')
        }
      })

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})